import java.util.*;

public class ControlerModel extends Models {
  private float _rotateLeft;
  private float _rotateRight;
  private float _translateUD;
  private float _translateLR;
  private RectControlerModel _left;
  private RectControlerModel _center;
  private RectControlerModel _right;
  private PortalModel _portal;
  private boolean _portalToRight;

  public ControlerModel(PVector centerP, boolean r) {
    super(centerP, 1, new PVector(60, 100, 1));
    this._portalToRight = r;
    this._rotateLeft = 0;
    this._rotateRight = 0;
    this._translateUD = 0;
    this._translateLR = 0; 
    this._left = new RectControlerModel(new PVector(this._pos.x, this._pos.y - this._size.y, this._pos.z), new PVector(20, this._size.y, 1), color(128, 128, 128));
    this._center = new RectControlerModel(this._pos, this._size, color(128, 128, 128));
    this._right = new RectControlerModel(new PVector(this._pos.x, this._pos.y + this._size.y, this._pos.z), new PVector(20, this._size.y, 1), color(128, 128, 128));
    if (this._portalToRight)
      this._portal = new PortalModel(new PVector(this._pos.x + 150, this._pos.y, this._pos.z - 50));
    else
      this._portal = new PortalModel(new PVector(this._pos.x - 150, this._pos.y, this._pos.z - 50));
  }

  public float getRotateLeft() {
    return this._rotateLeft;
  }

  public float getRotateRight() {
    return this._rotateRight;
  }

  public void setRotateLeft(float p) {
    this._rotateLeft = p;
  }

  public void setRotateRight(float p) {
    this._rotateRight = p;
  }

  public float getTranslateLR() {
    return this._translateLR;
  }

  public float getTranslateUD() {
    return this._translateUD;
  }

  public void setTranslateLR(float p) {
    this._translateLR = p;
  }

  public void setTranslateUD(float p) {
    this._translateUD = p;
  }

  public void draw() {
    this._left.draw();
    this._center.draw();
    this._right.draw();
    this._portal.draw();
  }

  public void update() {
    if (mousePressed) {
      if (this._center._boundingbox.inBoundingBox(new PVector(mouseX, mouseY, 0)) || this._center.isSelected()) {
        this._center.setSelected(true);
        this._portal.setColor(color(0, 0, 255));
        if (mouseY < pmouseY) {
          //trans left
          this._center.setTranslateLR(-5);
          this._left.setTranslateLR(-5);
          this._right.setTranslateLR(-5);
          this._portal.setTranslateLR(-5);
          this._left.update();
          this._center.update();
          this._right.update();
          this._portal.update();
        }
        if (mouseY > pmouseY) {
          //trans right
          this._center.setTranslateLR(5);
          this._left.setTranslateLR(5);
          this._right.setTranslateLR(5);
          this._portal.setTranslateLR(5);
          this._left.update();
          this._center.update();
          this._right.update();
          this._portal.update();
        }

        if (this._portalToRight) {
          if (mouseX < pmouseX) {
            //monter
            this._portal.setTranslateUD(5);
            this._portal.update();
          }
          else if (mouseX > pmouseX) {
            //descendre
            this._portal.setTranslateUD(-5);
            this._portal.update();
          }
        }
        else {
          if (mouseX < pmouseX) {
            //monter
            this._portal.setTranslateUD(-5);
            this._portal.update();
          }
          else if (mouseX > pmouseX) {
            //descendre
            this._portal.setTranslateUD(5);
            this._portal.update();
          }
        }
      }
      else if (this._left._boundingbox.inBoundingBox(new PVector(mouseX, mouseY, 0)) || this._left.isSelected() || this._right._boundingbox.inBoundingBox(new PVector(mouseX, mouseY, 0)) || this._right.isSelected()) {
        if (this._left._boundingbox.inBoundingBox(new PVector(mouseX, mouseY, 0)) || this._left.isSelected()) {
          this._left.setSelected(true);
          if (this._portalToRight) {
            if (pmouseX > mouseX)
              this._rotateLeft = this._rotateLeft + 1; 
            else if (pmouseX < mouseX)
              this._rotateLeft = this._rotateLeft - 1;
            if (this._rotateLeft > 20)
              this._rotateLeft = 20;
            if (this._rotateLeft < -20)
              this._rotateLeft = -20;
            this._portal.setColor(color(255, 125, 0));
            this._portal.setRotateLeft(this._rotateLeft);
          }
          else {
            if (pmouseX > mouseX)
              this._rotateRight = this._rotateRight + 1; 
            else if (pmouseX < mouseX)
              this._rotateRight = this._rotateRight - 1;
            if (this._rotateRight > 45)
              this._rotateRight = 45;
            if (this._rotateRight < -45)
              this._rotateRight = -45;
            this._portal.setColor(color(0, 255, 0));
            this._portal.setRotateRight(this._rotateRight);
          }
        }
        else if (this._right._boundingbox.inBoundingBox(new PVector(mouseX, mouseY, 0)) || this._right.isSelected()) {
          this._right.setSelected(true);
          if (this._portalToRight) {
            if (pmouseX > mouseX)
              this._rotateRight = this._rotateRight + 1; 
            else if (pmouseX < mouseX)
              this._rotateRight = this._rotateRight - 1;
            if (this._rotateRight > 45)
              this._rotateRight = 45;
            if (this._rotateRight < -45)
              this._rotateRight = -45;
            this._portal.setColor(color(0, 255, 0));
            this._portal.setRotateRight(this._rotateRight);
          }
          else {
            if (pmouseX > mouseX)
              this._rotateLeft = this._rotateLeft + 1; 
            else if (pmouseX < mouseX)
              this._rotateLeft = this._rotateLeft - 1;
            if (this._rotateLeft > 20)
              this._rotateLeft = 20;
            if (this._rotateLeft < -20)
              this._rotateLeft = -20;
            this._portal.setColor(color(255, 125, 0));
            this._portal.setRotateLeft(this._rotateLeft);
          }
        }
      }
      else {
        this._left.setSelected(false);        
        this._center.setSelected(false); 
        this._right.setSelected(false);
        this._portal.setColor(color(120, 120, 120));
      }
    }
    else {
      this._left.setSelected(false);        
      this._center.setSelected(false); 
      this._right.setSelected(false);
      this._portal.setColor(color(120, 120, 120));
    }
  }
}

